<?php

/**
 * Class for converting the View Data into XML.
 * Extends ViewHtmlElement: same rules apply for XML.
 *
 * @author Ian Clarence ian.clarence@gmail.com
 */ 
abstract class ViewXmlData extends ViewHtmlElement {
    
    /**
     * Flag: whether to use an XSL stylesheet or not.
     * 
     * @var bool
     */
    private $hasStylesheet = FALSE;

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) { 
        parent::__construct($fields);      
        $this->arrayToXml($this->fields);
    }
    
    /**
     * Set or unset the use of the stylesheet.
     * @param bool $hasStylesheet 
     */
    public function useStylesheet($hasStylesheet = TRUE) {
        $this->hasStylesheet = $hasStylesheet;
    }

    /**
     * Viewing the entire element.
     *
     * @return string
     */
    public function view() {

        /**
         * XML declaration.
         */
        $output = '<?xml version="1.0" encoding="' . CHARSET . '" ?>' . "\n";
        
        /**
         * XSL stylesheet. 
         */
        if ($this->hasStylesheet) {
            $stylesheetUrl = ViewUtils::addUrlComponent($this->fields['page']['full_url'], XSL_FILENAME);
            $output .= '<?xml-stylesheet type="text/xsl" href="' . $stylesheetUrl . '"?>' . "\n";            
        }

        /**
         * Normal viewing.
         */
        $output .= parent::view();

        /**
         * Final text string.
         */
        return $output;
    }
    
}